import Vue from 'vue'
import VueRouter from 'vue-router'

// components
import Login from './components/MyLogin.vue'
import Home from './components/MyHome.vue'

// Home's subComponents
import Users from './components/menus/MyUsers.vue'
import Rights from './components/menus/MyRights.vue'
import Goods from './components/menus/MyGoods.vue'
import Orders from './components/menus/MyOrders.vue'
import Settings from './components/menus/MySettings.vue'
import UserDetail from './components/user/MyUserDetail.vue'

Vue.use(VueRouter)

var router = new VueRouter({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        { 
            path: '/home', 
            component: Home, 
            // redirect: '/home/users',
            children: [
                { path: '', redirect: 'users' },
                { path: 'users', component: Users },
                { path: 'rights', component: Rights },
                { path: 'goods', component: Goods },
                { path: 'orders', component: Orders },
                { path: 'settings', component: Settings },
                { path: 'userinfo/:id', component: UserDetail, props: true }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    if(to.path === '/login' || localStorage.getItem('token')) {
        next()
        app()
    }
    else {
        next('/login')
    }
})

export default router